//
//  XBZuiXinCellFive.m
//  Home
//
//  Created by 陈明茂 on 16/4/4.
//  Copyright © 2016年 iOS. All rights reserved.
//

#import "XBZuiXinCellFive.h"

// 列数
#define col 2

@interface XBZuiXinCellFive ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *layout;


@end

@implementation XBZuiXinCellFive

- (void)awakeFromNib{
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.layout.itemSize = [self itemSize];
    self.collectionView.backgroundColor = [UIColor colorWithWhite:237 / 255.0 alpha:1];
    
}

- (void)layoutSubviews{
    
    [super layoutSubviews];
}

#pragma mark - 设置itemsize大小

- (CGSize)itemSize{
    
    CGFloat radio = (CGFloat)5 / 4;
    
    CGFloat margin = 0;
    // 4.7英寸
    if ([UIScreen mainScreen].bounds.size.width == 375) {
        margin = 10;
        
    }else if ([UIScreen mainScreen].bounds.size.width == 414){
        margin = 13;
    }
    self.layout.sectionInset = UIEdgeInsetsMake(8, margin, 8, margin);
    self.layout.minimumInteritemSpacing = margin;
    self.layout.minimumLineSpacing = margin;
    
    // 计算宽度
    CGFloat width = ([UIScreen mainScreen].bounds.size.width - margin * (col + 1)) / col;
    
    CGFloat height = width * radio;
    
    
    
    return CGSizeMake(width, height);
}

#pragma mark - collectionView的代理方法

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return 20;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"five_collection" forIndexPath:indexPath];
    cell.backgroundColor = [[UIColor alloc] initWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1];
    
    return cell;
}
- (void)setFrame:(CGRect)frame
{
    frame.size.height -= 8;
    [super setFrame:frame];
}
@end
